﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Game.Foundation.ChineseChess
{
    public class Pao : ChessmanBase
    {
        public Pao(Chessboard board, Camp camp, string text) : base(board, camp, text) { }

        public override bool CanMoveTo(int xLocation, int yLocation)
        {
            int xoffset = xLocation - this.XLocation;
            int yoffset = yLocation - this.YLocation;
            ChessmanBase dest = this._board.GetChessman(xLocation, yLocation);

            if (xoffset * yoffset != 0) return false;


            if (xoffset == 0)
            {
                if (dest != null)
                {
                    if (this._board.GetChessman(xLocation, yLocation).Camp != this.Camp)
                    {
                        int count = 0;
                        for (int begin = this.YLocation + yoffset / Math.Abs(yoffset); begin != yLocation; begin += yoffset / Math.Abs(yoffset))
                        {
                            if (this._board.GetChessman(xLocation, begin) != null) count++;
                        }

                        return count == 1;
                    }
                }
                else
                {
                    for (int begin = this.YLocation + yoffset / Math.Abs(yoffset); begin != yLocation; begin += yoffset / Math.Abs(yoffset))
                    {
                        if (this._board.GetChessman(xLocation, begin) != null) return false;
                    }

                    return true;
                }
            }
            else
            {
                if (dest != null)
                {
                    if (this._board.GetChessman(xLocation, yLocation).Camp != this.Camp)
                    {
                        int count = 0;
                        for (int begin = this.XLocation + xoffset / Math.Abs(xoffset); begin != xLocation; begin += xoffset / Math.Abs(xoffset))
                        {
                            if (this._board.GetChessman(begin, yLocation) != null) count++;
                        }

                        return count == 1;
                    }
                }
                else
                {
                    for (int begin = this.XLocation + xoffset / Math.Abs(xoffset); begin != xLocation; begin += xoffset / Math.Abs(xoffset))
                    {
                        if (this._board.GetChessman(begin, yLocation) != null) return false;
                    }

                    return true;
                }
            }

            return false;
        }
    }
}
